package com.zzh.lib.views.shine;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;

import com.zzh.lib.views.R;
import com.zzh.lib.views.def.OnAnimationEndListener;

import static com.zzh.lib.views.HTagFlowLayout.dip2px;

/**
 * Created by ZZH on 2023/2/16.
 *
 * @date: 2023/2/16 14:22
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.lib.views.shine
 */
public class HFollowGiftView extends View {
    private float rectRadius = 0f;
    private RectF rect = new RectF();
    private Paint rectPaint = new Paint();
    private Paint plusPaint = new Paint();
    private float plusLineWidth = 0f;
    private Path plusPath = new Path();

    private Paint okPaint = new Paint();
    private float okLineWidth = 0f;
    private Path okPath = new Path();
    private PathMeasure pathMeasure = null;
    private boolean startDrawOk = false;

    private ValueAnimator animatorDrawOk = null;

    private PathEffect effect = null;
    private boolean isAnimating = false;

    private int fillColor = 0xFFEE0051;

    public HFollowGiftView(Context context) {
        super(context);
        init(context);
    }

    public HFollowGiftView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HFollowGiftView);
        if (a.hasValue(R.styleable.HFollowGiftView_zh_fill_color)) {
            fillColor = a.getColor(R.styleable.HFollowGiftView_zh_fill_color, 0xFFEE0051);
        }
        a.recycle();
        init(context);
    }

    public HFollowGiftView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HFollowGiftView);
        if (a.hasValue(R.styleable.HFollowGiftView_zh_fill_color)) {
            fillColor = a.getColor(R.styleable.HFollowGiftView_zh_fill_color, 0xFFEE0051);
        }
        a.recycle();
        init(context);
    }

    private void init(Context context) {
        rectRadius = dip2px(context, 15f);
        plusLineWidth = dip2px(context, 2f);

        rectPaint.setAntiAlias(true);
        rectPaint.setColor(fillColor);
        rectPaint.setStyle(Paint.Style.FILL);

        plusPaint.setAntiAlias(true);
        plusPaint.setColor(Color.WHITE);
        plusPaint.setStyle(Paint.Style.STROKE);
        plusPaint.setStrokeWidth(plusLineWidth);

        okLineWidth = dip2px(context, 2.5f);
        okPaint.setAntiAlias(true);
        okPaint.setColor(fillColor);
        okPaint.setStyle(Paint.Style.STROKE);
        okPaint.setStrokeWidth(okLineWidth);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        float plusSize = getMeasuredHeight() * 0.6f;
        plusPath.moveTo(getMeasuredWidth() / 2f, (getMeasuredHeight() - plusSize) / 2);
        plusPath.lineTo(getMeasuredWidth() / 2f, (getMeasuredHeight() + plusSize) / 2);
        plusPath.moveTo((getMeasuredWidth() - plusSize) / 2, getMeasuredHeight() / 2f);
        plusPath.lineTo((getMeasuredWidth() + plusSize) / 2, getMeasuredHeight() / 2f);

        okPath.moveTo(getMeasuredWidth() * 0.31f, getMeasuredHeight() * 0.5f);
        okPath.lineTo(getMeasuredWidth() * 0.45f, getMeasuredHeight() * 0.75f);
        okPath.lineTo(getMeasuredWidth() * 0.68f, getMeasuredHeight() * 0.25f);
        pathMeasure = new PathMeasure(okPath, true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        rect.set(0f, 0f, getMeasuredWidth(), getMeasuredHeight());
        if (startDrawOk) {
            rectPaint.setColor(Color.WHITE);
            canvas.drawRoundRect(rect, rectRadius, rectRadius, rectPaint);
            canvas.drawPath(okPath, okPaint);
        } else {
            rectPaint.setColor(fillColor);
            canvas.drawRoundRect(rect, rectRadius, rectRadius, rectPaint);
            //加号
            canvas.drawPath(plusPath, plusPaint);
        }
    }

    public void startFollowAnimation(OnAnimationEndListener animationEnd) {
        isAnimating = true;
        ScaleAnimation startScaleAnimation = new ScaleAnimation(
                1f,
                1.2f,
                1f,
                1.2f,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f
        );
        startScaleAnimation.setDuration(300);
        startScaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {

            }

            public void onAnimationEnd(Animation animation) {
                startDrawOk = true;
                animatorDrawOk = ValueAnimator.ofFloat(1f, 0f);
                animatorDrawOk.setDuration(1000);
                animatorDrawOk.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue();
                        effect = new DashPathEffect(
                                new float[]{pathMeasure.getLength(), pathMeasure.getLength()},
                                value * pathMeasure.getLength()
                        );
                        okPaint.setPathEffect(effect);
                        invalidate();
                    }
                });
                animatorDrawOk.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {

                        AnimationSet animationSet = new AnimationSet(true);
                        AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0.2f);
                        ScaleAnimation scaleAnimation = new ScaleAnimation(
                                1f,
                                0.2f,
                                1f,
                                0.2f,
                                Animation.RELATIVE_TO_SELF,
                                0.5f,
                                Animation.RELATIVE_TO_SELF,
                                0.5f
                        );
                        animationSet.addAnimation(alphaAnimation);
                        animationSet.addAnimation(scaleAnimation);
                        animationSet.setDuration(300);
                        animationSet.setInterpolator(new AccelerateInterpolator());
                        animationSet.setAnimationListener(new Animation.AnimationListener() {
                            public void onAnimationStart(Animation animation) {

                            }

                            public void onAnimationEnd(Animation animation) {
                                startDrawOk = false;
                                isAnimating = false;
                                animationEnd.onAnimationEnd();

                            }

                            public void onAnimationRepeat(Animation animation) {

                            }
                        });
                        startAnimation(animationSet);
                    }
                });
                animatorDrawOk.start();
            }

            public void onAnimationRepeat(Animation animation) {
            }
        });
        startAnimation(startScaleAnimation);
    }

    public boolean isAnimating() {
        return isAnimating;
    }
}
